﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace EasyLibrary.AR.Classes
{
    public class ViaturaCollection : ActiveRecord
    {
        public int IDViatura { get; set; }
        public string Matricula { get; set; }
        public string Marca { get; set; }
        public string Modelo { get; set; }
        public string Tipo { get; set; }
        public int kit { get; set; }
        public Empresa Emp { get; set; }
        public Condutor Cond { get; set; }
        public DateTime DataUltimaActualizacao { get; set; }
        public float Velocidade { get; set; }
        public bool Ligado { get; set; }

        //
        public double lat { get; set; }
        public double lng { get; set; }
        public DateTime d { get; set; }

        public ViaturaCollection()
        {
        }

        public ViaturaCollection(double lati, double lngi,DateTime data)
        {
            this.lat = lati;
            this.lng = lngi;
            this.d = data;
        }

        public ViaturaCollection(string matr, string mark, string modelo, string tipo, int kit, Condutor cond, bool lig)
        {
            this.Matricula = matr;
            this.Marca = mark;
            this.Modelo = modelo;
            this.Tipo = tipo;
            this.Cond = cond;
            this.Ligado = Ligado;
        }

        protected ViaturaCollection(DataRow row)
        {
            this.Matricula = (string) row["matricula"];
            this.Marca = (string) row["Marca"];
            this.Modelo = (string) row["Modelo"];
            this.Tipo = (string) row["Tipo"];
            this.Cond = (Condutor) row["Condutor"];
        }

        public DataSet LoadAll(string k, string emp, string tipo)
        {
            int ki = Convert.ToInt32(k);
            int empr = Convert.ToInt32(emp);
            int t = Convert.ToInt32(tipo);
            try
            {
                DataSet ds = ExecuteQuery(GetConnection(false), "EXECUTE ListaViaturasGT " + ki + ", " +
                                                                empr + ", " + t);


                return ds;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Erro BD", ex);
            }
        }


        public IList<ViaturaCollection> getLatLongInTime(int idviatura, DateTime dataActual, DateTime datainserida)
        {
            List<ViaturaCollection> ret = new List<ViaturaCollection>();
            //double lat;
           // double lng;

            try
            {
                DataSet ds =
                    executeCommandOnDataSet(string.Format(CultureInfo.InvariantCulture,
                                                          "select lat,lng,data from GeoCodes where RefIDViaturas={0} and data between '{1}' and '{2}'",
                                                          idviatura, datainserida, dataActual));

                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    ViaturaCollection vc = new ViaturaCollection(this.lat,this.lng,this.d);
                    vc.lat = (double) r["lat"];
                    vc.lng = (double) r["lng"];
                    vc.d = (DateTime) r["data"];
                    ret.Add(vc);
                }
                return ret;

            }
            catch (Exception ex)
            {
                throw new ApplicationException("Erro BD", ex);
            }
        }


        public override void Save()
        {
            throw new NotImplementedException();
        }
    }
}